﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace RegTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CodeReg reg = new CodeReg(txRegCode.Text);
            txResult.Text = reg.Encode("true|" + dtToDate.Value.Date.ToString());
        }
    }
    class CodeReg
    {
        private byte[] _Key;
        private byte[] IV = new byte[] { 0xEE, 0xCE, 0x55, 0x78, 0x91, 0xAC, 0xCC, 0xFF };

        public CodeReg(string Key)
        {
            _Key = Encoding.UTF8.GetBytes(Key);
        }
        public string Decode(string Data)
        {
            string rs = null;
            Byte[] input = Convert.FromBase64String(Data);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                ICryptoTransform trans = des.CreateDecryptor(_Key, IV);
                Byte[] output = trans.TransformFinalBlock(input, 0, input.Length);
                rs = Encoding.UTF8.GetString(output);
            }
            catch (Exception ex)
            {
                throw new Exception("decode error", ex);
            }
            return rs;
        }
        public string Encode(string Data)
        {
            string rs = null;
            Byte[] input = Encoding.UTF8.GetBytes(Data);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                ICryptoTransform trans = des.CreateEncryptor(_Key, IV);
                Byte[] output = trans.TransformFinalBlock(input, 0, input.Length);
                rs = Convert.ToBase64String(output);
            }
            catch (Exception ex)
            {
                throw new Exception("encode error", ex);
            }
            return rs;
        }

    }
}
